package com.netty.tcp.ew.service.impl;

import com.netty.tcp.ew.mapper.BaseAreaMapper;
import com.netty.tcp.ew.domain.BaseArea;
import com.netty.tcp.ew.service.IBaseAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 区域信息Service业务层处理
 *
 * @author Mr.wu
 * @date 2023-09-24
 */
@Service
public class BaseAreaServiceImpl implements IBaseAreaService
{
    @Autowired
    private BaseAreaMapper baseAreaMapper;

    /**
     * 查询区域信息
     *
     * @param areaId 区域信息主键
     * @return 区域信息
     */
    @Override
    public BaseArea selectBaseAreaByAreaId(Integer areaId)
    {
        return baseAreaMapper.selectBaseAreaByAreaId(areaId);
    }

    /**
     * 查询区域信息列表
     *
     * @param baseArea 区域信息
     * @return 区域信息
     */
    @Override
    public List<BaseArea> selectBaseAreaList(BaseArea baseArea)
    {
        return baseAreaMapper.selectBaseAreaList(baseArea);
    }

    /**
     * 查询全部区域信息列表
     *
     * @param baseArea 区域信息
     * @return 区域信息
     */
    @Override
    public List<BaseArea> selectBaseAreaAllList(BaseArea baseArea)
    {
        return baseAreaMapper.selectBaseAreaAllList(baseArea);
    }

    @Override
    public boolean checkAreaNameUnique(BaseArea baseArea) {
        return false;
    }


    /**
     * 新增区域信息
     *
     * @param baseArea 区域信息
     * @return 结果
     */
    @Override
    public int insertBaseArea(BaseArea baseArea)
    {
        BaseArea info = baseAreaMapper.selectBaseAreaByAreaId(baseArea.getAreaPid());
        baseArea.setAncestors(info.getAncestors() + "," + baseArea.getAreaPid());
        return baseAreaMapper.insertBaseArea(baseArea);
    }

    @Override
    public int updateBaseArea(BaseArea baseArea) {
        return 0;
    }


    /**
     * 修改子元素关系
     *
     * @param areaId 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateAreaChildren(Integer areaId, String newAncestors, String oldAncestors)
    {
        List<BaseArea> children = baseAreaMapper.selectChildrenAreaById(areaId);
        for (BaseArea child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            baseAreaMapper.updateAreaChildren(children);
        }
    }

    /**
     * 批量删除区域信息
     *
     * @param areaIds 需要删除的区域信息主键
     * @return 结果
     */
    @Override
    public int deleteBaseAreaByAreaIds(Integer[] areaIds)
    {
        return baseAreaMapper.deleteBaseAreaByAreaIds(areaIds);
    }





}
